﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.IO;
using System.Windows.Forms;

namespace CodeRobot.DAL
{
    public class CodeHelper
    {
        CodeRobot.Utility.IniFile iniFile = new Utility.IniFile(Application.StartupPath + "\\config.ini");

        /// <summary>
        /// 获取数据库中的表
        /// </summary>
        /// <param name="strDBName">数据库名</param>
        public void GetTables(string strDBName,System.Windows.Forms.RichTextBox txtMessage)
        {
            try
            {
                //项目名
                string strProjectName = iniFile.GetString("BASE", "PROJECT", "");

                //获取路径
                string strBLLPath = iniFile.GetString("DIR", "BLL", "");
                string strDALPath = iniFile.GetString("DIR", "DAL", "");
                string strDBSqlHelperPath = iniFile.GetString("DIR", "DBSQLHELPER", "");
                string strModelPath = iniFile.GetString("DIR", "MODEL", "");
                string strUtilityPath = iniFile.GetString("DIR", "UTILITY", "");
                string strManagePath = iniFile.GetString("DIR", "MANAGE", "");
                string strAPIPath = iniFile.GetString("DIR", "API", "");
                string strAndroidPackage = iniFile.GetString("ANDROID", "PACKAGE", "");//获取Android包名
                string strAndroidPath = iniFile.GetString("ANDROID", "MODEL", "");//获取Android生成目录

                //生成数据库连接文件
                DBSqlHelper.CreateDBSqlHelperFile(strDBSqlHelperPath, strProjectName);

                //创建README文件
                ReadmeHelper.CreateBLLFile(strBLLPath, strProjectName);
                ReadmeHelper.CreateDALFile(strDALPath, strProjectName);
                ReadmeHelper.CreateDBSqlHelperFile(strDBSqlHelperPath, strProjectName);
                ReadmeHelper.CreateModelFile(strModelPath, strProjectName);
                ReadmeHelper.CreateUtilityFile(strUtilityPath, strProjectName);
                ReadmeHelper.CreateManageFile(strUtilityPath, strProjectName);

                MySqlConnection cn = new MySqlConnection(CodeRobot.DBSqlHelper.DBMySQLHelper.ConnectionMySQL());
                cn.Open();
                string strSql = "SELECT TABLE_NAME FROM `information_schema`.`COLUMNS` WHERE TABLE_SCHEMA='"+ strDBName + "' GROUP BY TABLE_NAME";
                MySqlCommand cmd = new MySqlCommand(strSql, cn);
                MySqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    string strTableName = dr["TABLE_NAME"].ToString();

                    CodeRobot.Utility.PublicValue.SaveMessage(Convert.ToDateTime(DateTime.Now).ToString("MM-dd HH:mm:ss") + " 正在处理：" + strTableName);
                    txtMessage.Text = CodeRobot.Utility.PublicValue.GetMessage();

                    //读取表中的字段
                    GetColumnList(strTableName);

                    //生成数据处理类
                    DALHelper.CreateDALClass(strDALPath, strProjectName, strTableName);

                    //生成业务逻辑类
                    BLLHelper.CreateBLLClass(strBLLPath, strProjectName, strTableName);

                    //生成后台代码文件
                    ManageHelper.CreateManageFile(strManagePath, strProjectName, strTableName);

                    //生成RESTAPI
                    RestAPIHelper.CreateRestAPIClass(strAPIPath, strProjectName, strTableName);

                    //生成RESTAPI
                    ModelHelper.CreateAndroidModelClass(strAndroidPath, strProjectName, strTableName, strAndroidPackage);

                }
                dr.Dispose();
                cn.Close();

                //创建Utility类库下文件
                UtilityHelper.CreateLogHelperFile(strUtilityPath,strProjectName);


            }
            catch (Exception ex)
            {
                CodeRobot.Utility.LogHelper.Error(typeof(CodeHelper), ex, "获取数据库中的表", "GetTables");
            }

        }

        /// <summary>
        /// 获取表中的字段
        /// </summary>
        /// <param name="strTableName">表名</param>
        public void GetColumnList(string strTableName)
        {
            try
            {
                string strModelPath = iniFile.GetString("DIR", "MODEL", "");//实体类路径
                string strProjectName = iniFile.GetString("BASE", "PROJECT", "");//项目名

                string strEntityVariableList = "";//实体变量
                string strEntityFunctionList = "";//实体方法

                MySqlConnection cn = new MySqlConnection(CodeRobot.DBSqlHelper.DBMySQLHelper.ConnectionMySQL());
                cn.Open();
                string strSql = "SELECT COLUMN_NAME,DATA_TYPE,COLUMN_TYPE,COLUMN_KEY,COLUMN_COMMENT,EXTRA,COLUMN_DEFAULT,CHARACTER_SET_NAME FROM `information_schema`.`COLUMNS` WHERE TABLE_NAME='" + strTableName + "'";
                MySqlCommand cmd = new MySqlCommand(strSql, cn);
                MySqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    string strColumnName = dr["COLUMN_NAME"].ToString();//字段列名
                    string strDataType = dr["DATA_TYPE"].ToString();//数据类型
                    strDataType = CodeRobot.Utility.StringHelper.GetCSharpDBType(strDataType);
                    string strColumnType = dr["COLUMN_TYPE"].ToString();//列类型长度
                    string strColumnKey = dr["COLUMN_KEY"].ToString();//是否是主键
                    string strColumnComment = dr["COLUMN_COMMENT"].ToString();//注释
                    string strExtra = dr["EXTRA"].ToString();//是否自增
                    string strColumnDefault = dr["COLUMN_DEFAULT"].ToString();//默认值
                    string strCharacterSetName = dr["CHARACTER_SET_NAME"].ToString();//字符集

                    //构造实体类变量
                    #region
                    string strDefineValue = "m_" + CodeRobot.Utility.StringHelper.GetTableNameUpper(strColumnName);

                    //定义变量
                    string strValue = "private " + strDataType + "  " + strDefineValue + ";\r\n";
                    strEntityVariableList += strValue;

                    //定义方法
                    string strFunction = "\r\n";
                    strFunction += "/// <summary>\r\n";
                    strFunction += "/// " + strColumnComment + "\r\n";
                    strFunction += "/// </summary>\r\n";
                    strFunction += "public " + strDataType + " " + CodeRobot.Utility.StringHelper.GetTableNameUpper(strColumnName) + "\r\n";
                    strFunction += "{\r\n";
                    strFunction += "get\r\n";
                    strFunction += "{\r\n";
                    strFunction += "return " + strDefineValue + ";\r\n";
                    strFunction += "}\r\n";
                    strFunction += "set\r\n";
                    strFunction += "{\r\n";
                    strFunction += "" + strDefineValue + " = value;\r\n";
                    strFunction += "}\r\n";
                    strFunction += "}\r\n";

                    strEntityFunctionList += strFunction;
                    #endregion

                }
                dr.Dispose();
                cn.Close();

                //生成实体类
                ModelHelper.CreateModelClass(strModelPath, strProjectName, strTableName, strEntityVariableList+ strEntityFunctionList);

            }
            catch (Exception ex)
            {
                CodeRobot.Utility.LogHelper.Error(typeof(CodeHelper), ex, "获取表中的字段", "GetColumnList");
            }

        }

        

    }
}
